<?php

namespace App\Services\Base\Services;

use App\Constant\FrontendConstant;
use App\Services\Base\Model\AppConfig;
use App\Services\Base\Interfaces\ConfigServiceInterface;

class ConfigService implements ConfigServiceInterface
{
    /**
     * 获取服务配置
     *
     * @param string $app
     * @return array
     */
    public function getServiceConfig(string $app): array
    {
        return config('services.' . $app, []);
    }

    /**
     * 网站名
     *
     * @return string
     */
    public function getName(): string
    {
        return config('app.name');
    }

    /**
     * ICP
     *
     * @return string
     */
    public function getIcp(): string
    {
        return config('jumofang.system.icp', '');
    }

    /**
     * 获取logo
     *
     * @return array
     */
    public function getLogo(): array
    {
        return [
            'logo' => config('jumofang.system.logo'),
            'white_logo' => config('jumofang.system.white_logo'),
        ];
    }

    /**
     * 用户默认头像
     * @return string
     */
    public function getMemberDefaultAvatar(): string
    {
        return config('jumofang.member.default_avatar');
    }

    /**
     * 获取默认的编辑器
     * @return string
     */
    public function getEditor(): string
    {
        return config('jumofang.system.editor', 'html');
    }


    /**
     * 缓存状态
     *
     * @return boolean
     */
    public function getCacheStatus(): bool
    {
        return (int)config('jumofang.system.cache.status') === FrontendConstant::YES;
    }

    /**
     * 缓存时间
     *
     * @return integer
     */
    public function getCacheExpire(): int
    {
        return (int)config('jumofang.system.cache.expire');
    }

    /**
     * 图片存储驱动
     *
     * @return string
     */
    public function getImageStorageDisk(): string
    {
        return config('jumofang.upload.image.disk');
    }

    /**
     * 图片存储路径
     *
     * @return string
     */
    public function getImageStoragePath(): string
    {
        return config('jumofang.upload.image.path');
    }

    /**
     * ai问卷上传存储路径
     *
     * @return string
     */
    public function getAiwenjuanStoragePath(): string
    {
        return config('jumofang.upload.file.path');
    }

    /**
     * ai问卷存储驱动
     *
     * @return string
     */
    public function getAiwenjuanStorageDisk(): string
    {
        return config('jumofang.upload.file.disk');
    }

    /**
     * 已开启的社交登录app
     *
     * @return array
     */
    public function getEnabledSocialiteApps(): array
    {
        $apps = config('jumofang.member.socialite');
        $list = [];
        foreach ($apps as $app) {
            if ((int)($app['enabled'] ?? 0) !== 1) {
                continue;
            }
            $list[] = $app;
        }
        return $list;
    }

    /**
     * jumofang系统配置
     *
     * @return array
     */
    public function getDesignerConfig(): array
    {
        return config('jumofang');
    }

    /**
     * 获取所有配置
     * @return array
     */
    public function all(): array
    {
        return AppConfig::query()->orderBy('sort')->get()->toArray();
    }

    /**
     * 检测配置是否存在
     * @param string $key
     * @return bool
     */
    public function isConfigExists(string $key): bool
    {
        return AppConfig::query()->where('key', $key)->exists();
    }

    /**
     * 写入配置
     * @param array $config
     */
    public function setConfig(array $config): void
    {
        $data = array_column($this->all(), 'key');
        foreach ($config as $key => $value) {
            if (!in_array($key, $data)) {
                continue;
            }
            AppConfig::query()->where('key', $key)->update(['value' => $value]);
        }
    }

    /**
     * 获取阿里云oss配置
     * @return array
     */
    public function getAliyunOssConfig(): array
    {
        return config('filesystems.disks.oss');
    }

    /**
     * 登录限制规则
     *
     * @return int
     */
    public function getLoginLimitRule(): int
    {
        return (int)config('jumofang.system.login.limit.rule');
    }

    /**
     * 短信配置
     * @return array
     */
    public function getSms(): array
    {
        return config('sms');
    }
}
